(function() {
	var app = angular.module('configurationCollectionApp', [ 'ui.bootstrap' ]);
	app.controller('configurationCollectionController',
					function($http, $scope, $log, $uibModal, $sce) {
						var controller = this;

						var basePath = $scope.getAppBasePath();
						//保存默认的数据采集频率
						 $scope.dataAcquisitionFrequency="";
						// 参数定义
						var tabFlag = false;
						controller.queryCondition = {
							"monitorIndex":"",
							"indexKpi":"",
							"hostIp":"",
							"hostName":"",
							"page" : 1,
							"size" : 10,
						};
						controller.paginationInfo = {
							"maxSize" : 5,
							"totalPages" : 0,
							"totalElements" : 0,
							"totalPages2" : 0,
							"totalElements2" : 0
						};
						
						//tab1查询事件
						controller.pageChanged = function() {
							loadData();
						}
						
						//tab2查询事件
						controller.pageChanged2 = function() {
							loadTab2Data();
						}
						// 完成全选全部选事件
						$("#check_all_tab2").click(
								function() {
									$(".check_item_tab2").prop("checked",
											$(this).prop("checked"));
								});
						// 选中单个元素，如果单个元素选择满了，那么第一个主元素也被选中
						$(document)
								.on(
										"click",
										".check_item_tab2",
										function() {
											if ($(".check_item_tab2:checked").length == $(".check_item_tab2").length) {
												$("#check_all_tab2").prop("checked",
														true);
											}
											if ($(".check_item_tab2:checked").length == 0) {
												$("#check_all_tab2").prop("checked",
														false);
											}
										});
						
						
						// 完成全选全部选事件
						$("#check_all").click(
								function() {
									$(".check_item").prop("checked",
											$(this).prop("checked"));
								});
						// 选中单个元素，如果单个元素选择满了，那么第一个主元素也被选中
						$(document)
								.on(
										"click",
										".check_item",
										function() {
											if ($(".check_item:checked").length == $(".check_item").length) {
												$("#check_all").prop("checked",
														true);
											}
											if ($(".check_item:checked").length == 0) {
												$("#check_all").prop("checked",
														false);
											}
										});
						
						// 检索部分下拉框取值
						$.ajax({
									type : "post",
									url : basePath
											+ "/get/getSelectOptionValue",
									dataType : "json",
									contentType : "application/json",
									success : function(data) {
										var resultJson = angular.fromJson(data);
										$scope.monitorIndex = resultJson.monitorIndex;//监控指标
										$scope.Index = ["全部","cpu-use","network-packet_loss","mem-cache_kb","mnt.mq.acc.num","ping不通","mnt.mysql.log.delay"];//指标KPI
										$scope.selectedKpiIndex = $scope.Index[0];
										// 手动刷新
										$scope.$apply();
									}
								});
						
						//数据刷新频率设置值事件
						$scope.setValue = function(name){
							$scope.selectselectedSiteThreeInitValueedSite = name;
						}
						
						//重置事件
						this.resetConfiguration = function(){
							loadData();
							}
						this.resetConfigurationTab2 = function(){
							loadTab2Data();
						}
						//数据采集频率下拉框值获取
						   $scope.ThreeSites = [
						                        {id : "5", name : "5分钟"},
								        	    {id : "10", name : "10分钟"},
								        	    {id : "15", name : "15分钟"},
								        	    {id : "20", name : "20分钟"},
								        	    {id : "25", name : "25分钟"},
								        	    {id : "30", name : "30分钟"},
								        	    {id : "35", name : "35分钟"},
								        	    {id : "40", name : "40分钟"},
								        	    {id : "45", name : "45分钟"},
								        	    {id : "50", name : "50分钟"},
								        	    {id : "55", name : "55分钟"},
								        	    {id : "60", name : "60分钟"},
								        	    ];
						   

						//视图刷新频率下拉框值获取
						   $scope.FourSites = [
						                        {id : "5",name : "5分钟"},
								        	    {id : "10",name : "10分钟"},
								        	    {id : "15",name : "15分钟"},
								        	    {id : "20",name : "20分钟"},
								        	    {id : "25",name : "25分钟"},
								        	    {id : "30",name : "30分钟"},
								        	    {id : "35",name : "35分钟"},
								        	    {id : "40",name : "40分钟"},
								        	    {id : "45",name : "45分钟"},
								        	    {id : "50",name : "50分钟"},
								        	    {id : "55",name : "55分钟"},
								        	    {id : "50",name : "60分钟"},
						           	];
						//告警次数阈值下拉框获取
						   $scope.FiveSites = [
						                        {name : "5次"},
						                        {name : "15次"},
								        	    {name : "20次"},
								        	    {name : "25次"},
								        	    {name : "30次"},
								        	    {name : "35次"},
								        	    {name : "40次"},
								        	    {name : "45次"},
								        	    {name : "50次"},
								        	    {name : "55次"},
								        	    {name : "60次"}
						           	];
						   
				   
							//告警次数阈值下拉框获取
							   $scope.moSites = [
							                        {id :"5",name : "5次"},
							                        {id :"15",name : "15次"},
									        	    {id :"20",name : "20次"},
									        	    {id :"25",name : "25次"},
									        	    {id :"30",name : "30次"},
									        	    {id :"35",name : "35次"},
									        	    {id :"40",name : "40次"},
									        	    {id :"45",name : "45次"},
									        	    {id :"50",name : "50次"},
									        	    {id :"55",name : "55次"},
									        	    {id :"60",name : "60次"}
							           	];
					    //超阈值告警频率下拉框支获取
						   $scope.sixSites = [
						                        {id : "5", name : "5分钟"},
						                        {id : "15", name : "15分钟"},
								        	    {id : "20", name : "20分钟"},
								        	    {id : "25", name : "25分钟"},
								        	    {id : "30", name : "30分钟"},
								        	    {id : "35", name : "35分钟"},
								        	    {id : "40", name : "40分钟"},
								        	    {id : "45", name : "45分钟"},
								        	    {id : "50", name : "50分钟"},
								        	    {id : "55", name : "55分钟"},
								        	    {id : "60", name : "60分钟"}
						           	];
						   
						// 检索结果部分下拉框的取值
						$.ajax({
							type : "post",
							url : basePath
									+ "/get/getSelectOptionValue",
							dataType : "json",
							contentType : "application/json",
							success : function(data) {
								var resultJson = angular.fromJson(data);
								//数据采集频率值获取
								//视图刷新频率值获取
								//告警次数阈值获取
								//超阈值告警频率值获取
								// 手动刷新
								$scope.$apply();
							}
						});
						
						// 点击tab2数据
						this.showSection2 = function() {
							tabFlag = true;
							$("#Section2").addClass("in").addClass("active");
							$("#Section1").removeClass("in").removeClass("active");
							$("#tab2").css("display","block");
							$("#tab1").css("display","none");
							loadTab2Data();
						}
						// 点击tab1数据
						this.showSection1 = function() {
							tabFlag = false;
							$("#Section1").addClass("in").addClass("active");
							$("#Section2").removeClass("in").removeClass("active");
							$("#tab2").css("display","none");
							$("#tab1").css("display","block");
							tab1Parm();
						}
						//查询事件
						this.search = function(){
							if(!tabFlag){//tab1的检索方法
								tab1Parm();
							}else{//tab2的检索方法
								controller.queryCondition.hostName = $scope.inputHostName;
								controller.queryCondition.hostIp = $scope.inputHostIp;
								loadTab2Data();
								
							}
						}
						// 检索部分重置事件
						this.reset = function() {
							//tab1检索条件
							$scope.selectedMonitorIndex = "";
							$scope.selectedKpiIndex = $scope.Index[0];
							
							//tab2检索条件
							$scope.inputHostName = "";
							$scope.inputHostIp = "";
						}
						
						//tab1保存配置
						this.saveConfigurationTab1 = function() {
							var basePath = $scope.getAppBasePath();
							var conditons = []; 
							var ids = "";
							//获取tableList里的值
							$.each($(".check_item:checked"), function() {
								var contectsObj = new Object();   
								contectsObj.id = $(this).parents("tr").find("td:eq(1)").text();
								contectsObj.monitorIndex = $(this).parents("tr").find("td:eq(2)").text() || "";
								contectsObj.kpi = $(this).parents("tr").find("td:eq(3)").text() || "";
//								if($(this).parents("tr").find("#selectedSiteThreeInitValue").val() != ""){
//									contectsObj.dataAcquisitionFrequency = $(this).parents("tr").find("#selectedSiteThreeInitValue").val().split(":")[1].split("分钟")[0]  || "";
//								}else{
//									contectsObj.dataAcquisitionFrequency = "";
//								}
								contectsObj.dataAcquisitionFrequency = $scope.selectselectedSiteThreeInitValueedSite;
								if($(this).parents("tr").find("#selectedSiteFiveInitValue").val() != ""){
									contectsObj.times = $(this).parents("tr").find("#selectedSiteFiveInitValue").val().split(":")[1].split("次")[0]  || "";
								}else{
									contectsObj.times = "";
								}
							    if($(this).parents("tr").find("#selectedSiteSixInitValue").val() != ""){
							    	contectsObj.minutes = $(this).parents("tr").find("#selectedSiteSixInitValue").val().split(":")[1].split("分钟")[0]  || "";
							    }else{
							    	contectsObj.minutes = "";
							    }
								conditons.push(contectsObj);
								ids += $(this).parents("tr").find("td:eq(1)").text()+",";
							})
							ids = ids.substring(0,ids.length-1);
							// 如果没有选中的话
							if(conditons != "0"){
							if (conditons == "" ) {
								alert("请勾选需要修改的配置");
								return;
							} else {
								if (confirm("确认修改名为["
										+ ids + "]的配置吗?")) {
									// 保存后台调用
									$.ajax({
												type : "POST",
												url : basePath
														+ "/data/updateCollectionConfigInfo",
												dataType : "json",
												contentType : "application/json",
												data : JSON.stringify(conditons),
												success : function(data) {
													if(data != ""){
														alert("更新成功");
													}
													loadData();
												}
											});

								}
							}
							}
						}
						
						
						
						//tab2保存配置
						this.saveConfigurationTab2 = function() {
							var basePath = $scope.getAppBasePath();
							var conditons = []; 
							var ids = "";
							//获取tableList里的值
							$.each($(".check_item_tab2:checked"), function() {
								var contectsObj = new Object();   
								contectsObj.id = $(this).parents("tr").find("td:eq(1)").text();
								contectsObj.hostIp = $(this).parents("tr").find("td:eq(2)").text() || "";
								contectsObj.hostName = $(this).parents("tr").find("td:eq(3)").text() || "";
								if($(this).parents("tr").find("#viewRefreshSiteId").val() != ""){
									contectsObj.refreshTime = $(this).parents("tr").find("#viewRefreshSiteId").val().split(":")[1]  || "";
								}else{
									contectsObj.refreshTime = "";
								}
								contectsObj.sytemParameterId =  contectsObj.id = $(this).parents("tr").find("td:eq(5)").text();
								conditons.push(contectsObj);
								ids += $(this).parents("tr").find("td:eq(1)").text()+",";
							})
							ids = ids.substring(0,ids.length-1);
							// 如果没有选中的话
							if(conditons != "0"){
							if (conditons == "" ) {
								alert("请勾选需要修改的配置");
								return;
							} else {
								if (confirm("确认修改名为["
										+ ids + "]的配置吗?")) {
									// 保存后台调用
									$.ajax({
												type : "POST",
												url : basePath
														+ "/data/updateCollectionConfigTab2Info",
												dataType : "json",
												contentType : "application/json",
												data : JSON.stringify(conditons),
												success : function(data) {
													if(data != ""){
														alert("更新成功");
													}
													loadTab2Data();
												}
											});

								}
							}
							}
						}
						// 程序初期化
						this.init = function() {
							//selectOptionValue();//查询下拉框的数据
							/*以上为原始代码 以下为修改后的*/
							getTreeData();//查询主机ztree
							queryDataAcquisitionFrequency(); //查询数据采集频率
						}

						// 程序初期化
						this.init();
						
						//数据列表的下拉框数据 获取
						function selectOptionValue(){
							$.ajax({
								type : "post",
								url : basePath
										+ "/data/getListOptionValue",
								dataType : "json",
								contentType : "application/json",
								success : function(data) {
									var resultJson = angular.fromJson(data);
									//视图刷新频率
									for(var i=0;i<resultJson.viewRefreshOptionValue.length;i++){
										$scope.FourSites.push(resultJson.viewRefreshOptionValue[i]);
									}
//									//数据采集频率
									var item = {id:resultJson.dataAcquisitionFrequency.name,name : resultJson.dataAcquisitionFrequency.name+"分钟"};
									$scope.ThreeSites.push(item);
									// 手动刷新
									$scope.$apply();
								}
							});
						}
						// 数据查询方法
						function loadData() {
							$.ajax({
										type : "POST",
										url : basePath
												+ "/data/selectConfiguationCollectionInfo",
										dataType : "json",
										contentType : "application/json",
										 data : JSON
										 .stringify(controller.queryCondition),
										success : function(data) {
											 var result = angular.fromJson(data);
											 controller.dataList = result.data;
											 //为数据据加单位
											 for(var i=0;i<controller.dataList.length;i++){
//												 $scope.selectedSiteFiveInitValue = controller.dataList[i].times;
												 controller.dataList[i].times =  controller.dataList[i].times + "次";
												 controller.dataList[i].minutes =  controller.dataList[i].minutes + "分钟";
												 $scope.selectselectedSiteThreeInitValueedSite = controller.dataList[i].dataAcquisitionFrequency;
												 controller.dataList[i].dataAcquisitionFrequency = controller.dataList[i].dataAcquisitionFrequency + "分钟";
											 }
											controller.paginationInfo.totalPages = result.totalPages;
											controller.paginationInfo.totalElements = result.totalElements;
											// 手动刷新
											$scope.$apply();
										}
									});
						}
						//检索tab2的数据
						function loadTab2Data(){
							$.ajax({
								type : "POST",
								url : basePath
										+ "/data/selectConfiguationCollectionTab2Info",
								dataType : "json",
								contentType : "application/json",
								 data : JSON
								 .stringify(controller.queryCondition),
								success : function(data) {
									 var result = angular.fromJson(data);
									 controller.dataTab2List = result.data;
									 controller.paginationInfo.totalPages2 = result.totalPages;
									 controller.paginationInfo.totalElements2 = result.totalElements;
									// 手动刷新
									$scope.$apply();
								}
							});
						}
						//tab1检索条件设置
						function tab1Parm(){
							if($scope.selectedMonitorIndex != undefined){
								controller.queryCondition.monitorIndex = $scope.selectedMonitorIndex;
							}else{
								controller.queryCondition.monitorIndex = "";
							}
							if($scope.selectedKpiIndex ==  "全部"){
								controller.queryCondition.indexKpi = "";
							}else{
								controller.queryCondition.indexKpi = $scope.selectedKpiIndex;
							}
							loadData();
						}
						

						/*	以上为原始分页显示的代码;以下为ztree展示的代码*/
						
							// 获取树状图数据
							function getTreeData() {
								var address = "";
								$.ajax({
									type : "POST",
									url : basePath + "/data/selectMonitoringIndex",
									dataType : "json",
									contentType : "application/json",
									success : function(data) {
										//修改数据添加父级节点“监控指标配置”
										var resultArrary = {
												"id"       : "0",
												"name"     : "监控指标配置",
												"isParent" : "true",
												"children" :JSON.parse(data),
										}
										var setting = {
											// 异步加载POST方法   异步加载需要解决进页面后直接全选 点击修改  但是此时树结构是不完整的问题；
											//思路:js做判断当全选时不走之前的修改方法，后添加一个修改所有的接口，接着会发现全选二级目录也会遇到类似问题
/*											//本人懒且树结构的数据量小我选择放弃异步加载，直接load整棵树，后期用异步加载请注意上述问题。（异步加载的后台方法保留可用）
 * 												async : {
												enable : true,
												dataType : "text",
												type : "post",
												url : basePath + "/data/asyncGetKPITreeData",
												autoParam : [ "id" ],
												dataFilter : ajaxDataFilter
											},*/
											// checkBox 效果
											check : {
												enable : true,
												chkStyle : "checkbox"
											},
											callback : {
												onClick : zTreeOnClick,  //点击节点事件
												//onExpand: zTreeOnExpand  //点击节点前+号加载
											}
										};

										// 返回数据转成json
										function ajaxDataFilter(treeId, parentNode, responseData) {
											responseData = JSON.parse(responseData);
											return responseData;
										}
										;
										var zTreeObj = $.fn.zTree.init($("#hostztree"), setting,
												resultArrary);
										//点击展开第一级
										$("#hostztree_1_switch").click();
										
										//点击节点方法加载kpi的详细配置信息
										function zTreeOnClick(event, treeId, treeNode) {
											if(treeNode.isParent == false){//判断为末级子节点
												//将当前点击选中的节点配置信息添加到下拉框展示
												//告警次数阈值赋值
												$("#selectedSiteFiveInitValue").val(treeNode.times);
												//超阈值告警频率赋值
												$("#selectedSiteSixInitValue").val(treeNode.minutes);
											}
										}	//点击节点方法 结束
										
									}
								});
					}//getTreeData结束
							
							
							//查询数据采集频率
							function queryDataAcquisitionFrequency(){
								$.ajax({
									type : "post",
									url : basePath+ "/data/queryDataAcquisitionFrequency",
									dataType : "json",
									contentType : "application/json",
									success : function(data) {
										var resultJson = angular.fromJson(data);
										//数据采集频率默认值赋值
										$("#ThreeSitesDefault").val(data);
										$scope.dataAcquisitionFrequency=data;
									}
								});
							}//查询数据采集频率结束
							
							//修改数据采集频率 
							this.updataDataAcquisitionFrequency = function() {
								//获取当前选中的值
								var dataAcquisitionFrequency =$("#ThreeSitesDefault").val();
								if(dataAcquisitionFrequency==$scope.dataAcquisitionFrequency){
									alert("请选择修改频率");
									return;
								}
								// 如果没有选中的话
								if(dataAcquisitionFrequency != "0"){
								if (dataAcquisitionFrequency == "" ) {
									alert("请勾选需要修改的配置");
									return;
								} else {
									if (confirm("确认修改数据采集频率？")) {
										// 保存后台调用
										$.ajax({
											type : "POST",
											url : basePath+ "/data/updataDataAcquisitionFrequency",
											dataType : "json",
											data : {
												"dataAcquisitionFrequency":dataAcquisitionFrequency
											},
											success : function(data) {
												if(data != ""){
													alert("更新成功");
												}
												getTreeData();//查询主机ztree
											}
										});

									}
								}
								}
							}//修改数据采集频率结束

							//批量修改times AND minutes
							this.updataTimesANDMinutes = function() {
								var conditons = []; 
								var ids = "";
								var names="";
								//获取ztree选中的节点
					            var treeObj=$.fn.zTree.getZTreeObj("hostztree");
					            var nodes=treeObj.getCheckedNodes(true);
					            // 如果没有选中的话
								if(nodes.length==0){
									alert("请选择需要修改的KPI！");
									return;
								}
								if($("#selectedSiteFiveInitValue").val() == "" || 
										$("#selectedSiteFiveInitValue").val() ==null || 
										$("#selectedSiteFiveInitValue").val() == undefined ){
									alert("请选择告警次数阈值！");
									return;
								}
								if($("#selectedSiteSixInitValue").val() == "" || 
										$("#selectedSiteSixInitValue").val() ==null || 
										$("#selectedSiteSixInitValue").val() == undefined ){
									alert("请选择超阈值告警频率！");
									return;
								}
					            for(var i=0;i<nodes.length;i++){ //循环选中的节点
					            	if(nodes[i].isParent){ //父级节点不操作
					            		
					            	}else{ //末级节点KPI
					            		ids+=nodes[i].alarmFrequencyId + ",";//获取选中节点的值
					            		names+=nodes[i].name + ",";
					            		var contectsObj = new Object();   
					            		contectsObj.id = nodes[i].alarmFrequencyId;
					            		contectsObj.times =$("#selectedSiteFiveInitValue").val();
					            		contectsObj.minutes = $("#selectedSiteSixInitValue").val();
					            		conditons.push(contectsObj);
					            	}
					            }
								ids = ids.substring(0,ids.length-1);
								names = names.substring(0,names.length-1);
								//获取当前选中的值
								var dataAcquisitionFrequency =$("#ThreeSitesDefault").val();
	
								if (confirm("确认修改名为["
											+ names + "]的配置吗?")) {
										// 保存后台调用
										$.ajax({
											type : "POST",
											url : basePath+ "/data/updataTimesANDMinutes",
											dataType : "json",
											contentType : "application/json",
											data : JSON.stringify(conditons),
											success : function(data) {
												if(data != ""){
													alert("更新成功");
												}
												getTreeData();//查询主机ztree
											}
										});
									}
							}//批量修改times AND minues
	
})//app.controller结束
					
						/*以上为修改代码；以下为源代码*/
					
			.controller(
					'servicePopuController',
					function($scope, $uibModalInstance, $http, $log) {
						$scope.load = function() {
							// 原系统数据前后台交互
							var basePath = $scope.getAppBasePath();
							$scope.serviceInfo.HostSequenceNumber = "";
							$scope.serviceInfo.ip = "";
							$scope.serviceInfo.hostName = "";
							$scope.serviceInfo.useResponsibilityDepartment = "";
							$scope.serviceInfo.useResponsibility = "";
							$scope.serviceInfo.maintenanceResponsibleDepartment = "";
							$scope.serviceInfo.maintenanceResponsiblePerson = "";
							$.ajax({
										type : "POST",
										url : basePath + "/get/getServiceInfo",
										dataType : "json",
										contentType : "application/json",
										data : JSON.stringify($scope.parmeater),
										success : function(data) {
											var jsonResult = angular
													.fromJson(data);
											$scope.serviceInfo.HostSequenceNumber = jsonResult.HostSequenceNumber;
											$scope.serviceInfo.ip = jsonResult.ip;
											$scope.serviceInfo.hostName = jsonResult.hostName;
											$scope.serviceInfo.useResponsibilityDepartment = jsonResult.useResponsibilityDepartment;
											$scope.serviceInfo.useResponsibility = jsonResult.useResponsibility;
											$scope.serviceInfo.maintenanceResponsibleDepartment = jsonResult.maintenanceResponsibleDepartment;
											$scope.serviceInfo.maintenanceResponsiblePerson = jsonResult.maintenanceResponsiblePerson;
											$scope.$apply();
										}
									});

						};
						$scope.cancel = function() {
							$uibModalInstance.dismiss('cancel');
						};
					})
})();